<template>
  <div class="app-container">
    <!-- 顶部导航栏 -->
    <header class="navbar">
      <div class="container">
        <div class="navbar-brand">
          <router-link to="/" class="logo">
            <span class="logo-text">这部动漫</span>
          </router-link>
        </div>
        
        <!-- 桌面导航菜单 -->
        <nav class="navbar-nav">
          <router-link to="/rankings" class="nav-item" :class="{ active: $route.name === 'Rankings' }">排行榜</router-link>
          <router-link to="/all-animes" class="nav-item" :class="{ active: $route.name === 'AllAnimes' }">全部动漫</router-link>
          <router-link to="/nominate" class="nav-item" :class="{ active: $route.name === 'Nominate' }">提名</router-link>
          <router-link to="/profile" class="nav-item" :class="{ active: $route.name === 'Profile' }">我的</router-link>
        </nav>
        
        <!-- 用户操作区 -->
        <div class="user-actions">
          <div v-if="user" class="user-info">
            <!-- <div class="avatar-container">
              <img 
                v-if="user.avatar"
                class="avatar"
                :src="user.avatar"
                :alt="user.username + '的头像'"
                onerror="this.onerror=null;this.remove();"
              />
              <div v-else class="text-avatar" :style="getAvatarStyle()">
                {{ user.username ? user.username.charAt(0) : '?' }}
              </div>
            </div> -->
            <span class="username">{{ user.username }}</span>
            <button class="logout-btn" @click="logout">退出</button>
          </div>
          <div v-else>
            <router-link to="/login" class="login-btn">登录</router-link>
            <router-link to="/register" class="register-btn">注册</router-link>
          </div>
        </div>
        
        <!-- 移动端菜单按钮 -->
        <button class="menu-button" @click="toggleMobileMenu" aria-label="菜单">
          ☰
        </button>
      </div>
    </header>
    
    <!-- 移动端导航菜单 -->
    <div class="mobile-nav" v-if="isMobileMenuOpen">
      <nav class="mobile-nav-inner">
        <router-link to="/rankings" class="mobile-nav-item" :class="{ active: $route.name === 'Rankings' }" @click="toggleMobileMenu">排行榜</router-link>
        <router-link to="/all-animes" class="mobile-nav-item" :class="{ active: $route.name === 'AllAnimes' }" @click="toggleMobileMenu">全部动漫</router-link>
        <router-link to="/nominate" class="mobile-nav-item" :class="{ active: $route.name === 'Nominate' }" @click="toggleMobileMenu">提名</router-link>
        <router-link to="/profile" class="mobile-nav-item" :class="{ active: $route.name === 'Profile' }" @click="toggleMobileMenu">我的</router-link>
        <router-link v-if="!user" to="/login" class="mobile-nav-item" @click="toggleMobileMenu">登录</router-link>
        <router-link v-if="!user" to="/register" class="mobile-nav-item" @click="toggleMobileMenu">注册</router-link>
        <button v-if="user" class="mobile-nav-item logout-btn" @click="logout">退出</button>
      </nav>
    </div>
    
    <!-- 主要内容区域 -->
    <main class="main-content">
      <div class="container">
        <slot></slot>
      </div>
    </main>
    
    <!-- 页脚 -->
    <footer class="footer">
      <div class="container">
        <div class="footer-content">
          <div class="footer-info">
            <h3 class="footer-title">这部动漫</h3>
            <p class="footer-description">月度社区投票决定的动漫排行榜网站</p>
          </div>
          <div class="footer-links">
            <a href="/" class="footer-link">关于我们</a>
            <a href="/" class="footer-link">使用条款</a>
            <a href="/" class="footer-link">隐私政策</a>
          </div>
          <div class="footer-copyright">
            <p>© 2025 这部动漫. 保留所有权利.</p>
            <p class="icp-record"><a href="https://beian.miit.gov.cn/" target="_blank">赣ICP备2022001112号-1</a></p>
          </div>
        </div>
      </div>
    </footer>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'

// 用户状态
const user = ref<any>(null)
const isMobileMenuOpen = ref(false)
const router = useRouter()

// 检查用户登录状态
const checkUserStatus = () => {
  const storedUser = localStorage.getItem('user')
  if (storedUser) {
    user.value = JSON.parse(storedUser)
    localStorage.setItem('isLoggedIn', 'true')
  } else {
    localStorage.removeItem('isLoggedIn')
  }
}

// 退出登录
const logout = () => {
  localStorage.removeItem('user')
  localStorage.removeItem('authorization')
  localStorage.removeItem('isLoggedIn')
  user.value = null
  router.push('/login')
}

// 添加一个全局的用户状态更新函数，可以从任何组件调用
window.updateUserStatus = () => {
  checkUserStatus()
}

// 监听localStorage变化，自动更新用户状态
const handleStorageChange = (e: StorageEvent) => {
  if (e.key === 'user' || e.key === 'isLoggedIn') {
    checkUserStatus()
  }
}

// 切换移动端菜单
const toggleMobileMenu = () => {
  isMobileMenuOpen.value = !isMobileMenuOpen.value
}

// 监听窗口大小变化
const handleResize = () => {
  if (window.innerWidth > 768) {
    isMobileMenuOpen.value = false
  }
}

// 点击外部关闭移动端菜单
const handleClickOutside = (event: MouseEvent) => {
  const mobileNav = document.querySelector('.mobile-nav')
  const menuButton = document.querySelector('.menu-button')
  
  if (mobileNav && menuButton && !mobileNav.contains(event.target as Node) && !menuButton.contains(event.target as Node)) {
    isMobileMenuOpen.value = false
  }
}

// 已移除未使用的函数

// 组件挂载时执行
onMounted(() => {
  checkUserStatus()
  window.addEventListener('resize', handleResize)
  document.addEventListener('click', handleClickOutside)
  window.addEventListener('storage', handleStorageChange)
})

// 组件卸载时清理
onUnmounted(() => {
  window.removeEventListener('resize', handleResize)
  document.removeEventListener('click', handleClickOutside)
  window.removeEventListener('storage', handleStorageChange)
})
</script>

<style scoped>
/* 整体容器 */
.app-container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: #f2f2f7;
}

/* 顶部导航栏 - 苹果风格毛玻璃效果 */
.navbar {
  background-color: rgba(255, 255, 255, 0.8);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-bottom: 1px solid rgba(229, 229, 234, 0.5);
  position: sticky;
  top: 0;
  z-index: 1000;
}

.navbar .container {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 44px;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 16px;
  box-sizing: border-box;
}

/* 品牌Logo */
.navbar-brand {
  display: flex;
  align-items: center;
}

.logo {
  display: flex;
  align-items: center;
}

/* 用户头像样式 */
.avatar-container {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.avatar {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.text-avatar {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 600;
  color: white;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

.logo-text {
  font-size: 1.25rem;
  font-weight: 700;
  color: #007AFF;
  text-decoration: none;
  letter-spacing: -0.01em;
}

/* 桌面导航菜单 */
.navbar-nav {
  display: flex;
  align-items: center;
  gap: 24px;
}

.nav-item {
  font-weight: 500;
  color: #1C1C1E;
  padding: 8px 12px;
  border-radius: 10px;
  transition: all 0.2s ease;
  text-decoration: none;
}

.nav-item:hover {
  background-color: rgba(0, 0, 0, 0.05);
  color: #007AFF;
}

.nav-item.active {
  color: #007AFF;
  background-color: rgba(0, 122, 255, 0.1);
}

/* 用户操作区 */
.user-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

/* 用户信息布局 */
.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
}

.username {
  font-weight: 500;
  color: #1C1C1E;
}

.login-btn,
.register-btn {
  font-weight: 500;
  padding: 8px 16px;
  border-radius: 10px;
  transition: all 0.2s ease;
  text-decoration: none;
  border: none;
  cursor: pointer;
  font-size: 1rem;
}

.login-btn {
  color: #007AFF;
  background: none;
}

.login-btn:hover {
  background-color: rgba(0, 122, 255, 0.1);
}

.register-btn {
  background-color: #007AFF;
  color: white;
  padding: 8px 16px;
}

.register-btn:hover {
  background-color: #0062CC;
}

.logout-btn {
  background-color: #FF3B30;
  color: white;
  padding: 8px 16px;
  border-radius: 10px;
  transition: all 0.2s ease;
  text-decoration: none;
  border: none;
  cursor: pointer;
  font-size: 1rem;
  font-weight: 500;
}

.logout-btn:hover {
  background-color: #CC0000;
}

/* 移动端菜单按钮 */
.menu-button {
  display: none;
  background: none;
  border: none;
  font-size: 1.5rem;
  color: #1C1C1E;
  cursor: pointer;
  padding: 8px;
  border-radius: 10px;
  transition: background-color 0.2s ease;
}

.menu-button:hover {
  background-color: rgba(0, 0, 0, 0.05);
  transform: none;
  box-shadow: none;
}

/* 移动端导航菜单 */
.mobile-nav {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 2000;
  display: flex;
  justify-content: flex-end;
  animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.mobile-nav-inner {
  background-color: white;
  width: 280px;
  max-width: 80vw;
  height: 100%;
  overflow-y: auto;
  animation: slideInRight 0.3s ease;
  border-top-left-radius: 20px;
  border-bottom-left-radius: 20px;
  padding: 20px;
}

@keyframes slideInRight {
  from {
    transform: translateX(100%);
  }
  to {
    transform: translateX(0);
  }
}

.mobile-nav-item {
  display: block;
  width: 100%;
  padding: 12px 16px;
  margin-bottom: 8px;
  border-radius: 10px;
  font-weight: 500;
  color: #1C1C1E;
  text-align: left;
  transition: all 0.2s ease;
  text-decoration: none;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1rem;
}

.mobile-nav-item:hover {
  background-color: rgba(0, 0, 0, 0.05);
  color: #007AFF;
}

.mobile-nav-item.active {
  color: #007AFF;
  background-color: rgba(0, 122, 255, 0.1);
}

/* 主要内容区域 */
.main-content {
  flex: 1;
  padding: 24px 0;
}

.main-content .container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 16px;
  box-sizing: border-box;
}

/* 页脚 */
.footer {
  background-color: rgba(255, 255, 255, 0.8);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-top: 1px solid rgba(229, 229, 234, 0.5);
  padding: 24px 0;
}

.footer .container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 16px;
  box-sizing: border-box;
}

.footer-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
}

.footer-info {
  margin-bottom: 16px;
}

.footer-title {
  font-size: 1.25rem;
  font-weight: 700;
  color: #1C1C1E;
  margin-bottom: 8px;
  letter-spacing: -0.01em;
}

.footer-description {
  color: #6C6C70;
  font-size: 0.875rem;
  margin-bottom: 16px;
}

.footer-links {
  display: flex;
  gap: 20px;
  margin-bottom: 16px;
}

.footer-link {
  color: #007AFF;
  font-size: 0.875rem;
  transition: color 0.2s ease;
  text-decoration: none;
}

.footer-link:hover {
  color: #0062CC;
}

.footer-copyright {
  color: #8E8E93;
  font-size: 0.75rem;
}

.icp-record {
  margin-top: 4px;
  color: #8E8E93;
  font-size: 0.75rem;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .navbar-nav,
  .user-actions {
    display: none;
  }
  
  .menu-button {
    display: block;
  }
  
  .main-content {
    padding: 16px 0;
  }
  
  .footer-links {
    flex-direction: column;
    gap: 8px;
  }
}

/* 高对比度模式支持 */
@media (prefers-contrast: high) {
  .navbar {
    background-color: #ffffff;
    border-bottom: 2px solid #000000;
  }
  
  .mobile-nav-inner {
    background-color: #ffffff;
    border: 2px solid #000000;
  }
  
  .footer {
    background-color: #ffffff;
    border-top: 2px solid #000000;
  }
  
  .login-btn,
  .register-btn,
  .logout-btn {
    border: 2px solid #000000;
  }
}

/* 减少动画偏好支持 */
@media (prefers-reduced-motion: reduce) {
  .mobile-nav,
  .mobile-nav-inner {
    animation: none;
  }
  
  .mobile-nav-inner {
    transform: none;
  }
}
</style>